package com.hys.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hys.dao.CoopDetailMapper;
import com.hys.entity.CoopDetail;
import com.hys.exception.ServiceException;
import com.hys.param.CooperAddOrUpdateParam;
import com.hys.service.CoopDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CoopDetailServiceImpl extends ServiceImpl<CoopDetailMapper, CoopDetail> implements CoopDetailService {

    @Override
    public String getJsonByInfoId(Integer infoId) {
        QueryWrapper<CoopDetail> qw = new QueryWrapper<>();
        qw.lambda().eq(CoopDetail::getInfoId, infoId);
        CoopDetail coopDetail = getOne(qw);
        if(coopDetail == null){
            log.error("当前ID值：{}", infoId);
            throw new ServiceException("未查询到当前详情记录");
        }
        return coopDetail.getDeatil();
    }

    @Override
    public void add(Integer infoId, String detail) {
        CoopDetail coopDetail = new CoopDetail();
        coopDetail.setDeatil(detail);
        coopDetail.setInfoId(infoId);
        save(coopDetail);
    }

    @Override
    public void updateByInfoId(CooperAddOrUpdateParam addOrUpdateParam) {
        String jsonStr = JSONUtil.toJsonStr(addOrUpdateParam);
        UpdateWrapper<CoopDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(CoopDetail::getInfoId, addOrUpdateParam.getId()).set(CoopDetail::getDeatil, jsonStr);
        update(updateWrapper);
    }
}
